package com.szxy.asynctest;

import org.junit.jupiter.api.Test;

import java.util.Random;
import java.util.RandomAccess;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author: zwz
 * @Date:2020/10/21 9:06
 * @Description:com.szxy.asynctest
 * @Version:1.0
 * CompletableFuture 接口
 **/
public class AsynTest {

    @Test
    public void generateUUID(){
        Random random = new Random();
        
    }

    @Test
    public void asyncThread() throws Exception {
        CompletableFuture async1 = CompletableFuture.runAsync(() -> {
            try {
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName());
                System.out.println("none return Async");
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        // 调用get()将等待异步逻辑处理完成
        async1.get();
    }


    @Test
    public void asyncThread2() throws Exception {
        CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName());
            return "HelloWorld";
        });
        String result = cf.get();
        System.out.println("result = " + result);
    }

    @Test
    public void asyncThread3() throws Exception {
        CompletableFuture<String> a = CompletableFuture.supplyAsync(() -> "hello");
        CompletableFuture<String> b = CompletableFuture.supplyAsync(() -> "youth");
        CompletableFuture<String> c = CompletableFuture.supplyAsync(() -> "!");

        CompletableFuture all = CompletableFuture.allOf(a, b, c);
        all.get();

        String result = Stream.of(a, b, c)
                .map(CompletableFuture::join)
                .collect(Collectors.joining(" "));

        System.out.println(result);
    }

    @Test
    public void asyncThread4() throws Exception {
        CompletableFuture<String> a = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(20);
                return "hello";
            } catch (Exception e) {
                e.printStackTrace();
                return "none~";
            }
        });
        CompletableFuture<String> b = CompletableFuture.supplyAsync(() -> "youth");
        CompletableFuture<String> c = CompletableFuture.supplyAsync(() -> "!");

        CompletableFuture<Object> any = CompletableFuture.anyOf(a, b, c);
        String result = (String) any.get();

        System.out.println(result);
    }

}
